
{% macro help_text(field, tag="span") %}
      {%- if field.help_text -%}
        <{{tag}} class="field-help_text">
          {{ field.help_text }}
        </{{tag}}>
      {%- endif -%}
{% endmacro %}

{% macro render_form_fields(form) %}
{% if form.non_field_errors() %}
{{ form.non_field_errors() }}
{% endif %}
{% for hidden in form.hidden_fields() %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields() %}
  {{ render_field(field) }}
{% endfor %}
{% endmacro %}

{% macro render_field(field) %}
<div class="field {{ field.html_name }}
  {%- if field.errors %} errors{% endif %}">
  {% if field.errors %}
  {{ field.errors }}
  {% endif %}
  <label class="field-wrapping_label">
    {%- if field.field.__class__.__name__ == 'BooleanField' %}
      <ul class="checkbox_options options_list">
        <li>
          <label class="field-option_label">
            <input type="checkbox" name="{{ field.html_name }}"
              {%- if field.value() %} checked="checked"{% endif %}>
            <span class="option-display_text">
              {{ field.label }}
            </span>
          </label>
        </li>
      </ul>
    {% elif field.field.widget.__class__.__name__ in ["CheckboxInput", "CheckboxSelectMultiple", "RadioSelect"] %}

    <fieldset>
      <legend>
        {{ field.label }}
      </legend>
      {% if field.help_text %}
      <div class="field-help_text">
        {{ field.help_text }}
      </div>
      {% endif %}
      {% if field.field.widget.__class__.__name__ == "CheckboxInput" %}
      <ul class="checkbox_options options_list">
      {%- for option_key, option_label in field.field.choices %}
        <li>
          <label class="field-option_label">
            <input type="checkbox" name="{{ field.html_name }}" value="{{ option_key }}"
              {%- if option_key == field.value() %} checked="checked"{% endif %}>
            <span class="option-display_text">
              {{ option_label }}
            </span>
          </label>
        </li>
      {%- endfor %}
      </ul>
      {% elif field.field.widget.__class__.__name__ == "CheckboxSelectMultiple" %}
      <ul class="checkbox_options options_list">
      {%- for option_key, option_label in field.field.choices %}
        <li>
          <label class="field-option_label">
            <input type="checkbox" name="{{ field.html_name }}" value="{{ option_key }}"
              {%- if option_key in field.value() %} checked="checked"{% endif %}>
            <span class="option-display_text">
              {{ option_label }}
            </span>
          </label>
        </li>
      {%- endfor %}
      </ul>
      {% elif field.field.widget.__class__.__name__ == "RadioSelect" %}
      <ul class="radio_options options_list">
      {%- for option_key, option_label in field.field.choices %}
        <li>
          <label class="field-option_label">
            <input type="radio" name="{{ field.html_name }}" value="{{ option_key }}"
              {%- if option_key == field.value() %} checked="checked"{% endif %}>
            <span class="option-display_text">
              {{ option_label }}
            </span>
          </label>
        </li>
      {%- endfor %}
      </ul>
      {% endif %}

    </fieldset>
    {% else %}
      <span class="field-display_text">
        {{ field.label }}
      </span>
      {% if field.help_text %}
        <span class="field-help_text">
          {{ field.help_text }}
        </span>
      {% endif %}
      {{ field }}
    {% endif %}
  </label>
</div>
{% endmacro %}

{% macro render_warnings(warnings) %}
{%- if warnings -%}
<ul class="warninglist">
{%- for warning in warnings %}
  <li class="warning">
    {{ warning }}
  </li>
{%- endfor -%}
</ul>
{%- endif -%}
{% endmacro %}


{% macro render_errors(errors) %}
{%- if errors -%}
<ul class="errorlist">
{%- for error in errors %}
  <li class="error">
    {{ error }}
  </li>
{%- endfor -%}
</ul>
{%- endif -%}
{% endmacro %}


{% macro checkbox_options_field(field) %}
<div class="field field-checkbox_options {{ field.class_name() }}
  {%- if field.field_warnings() %} warnings
  {%- endif %}
  {%- if field.field_errors() %} errors{% endif%}">
  {{ render_errors(field.field_errors()) -}}
  {{ render_warnings(field.field_warnings()) -}}
  <legend>
    {{ field.label }}
  </legend>
  {{- help_text(field, tag="div") }}
  <ul class="checkbox_options options_list">
  {%- for option_key, option_label in field.choices.items() %}
  <li>
    <label class="field-option_label">
      <input type="checkbox" name="{{ field.field_name }}" value="{{ option_key }}"
        {%- if field.current_value() %}
          {%- if option_key in field.current_value() %} checked="checked"{% endif %}
        {%- endif -%}>
      <span class="option-display_text">
        {{ option_label }}
      </span>
    </label>
  </li>
  {% endfor -%}
  </ul>
</div>
{% endmacro %}


{% macro radio_select_field(field) %}
<div class="field field-radio_options {{ field.class_name() }}
  {%- if field.field_warnings() %} warnings
  {%- endif %}
  {%- if field.field_errors() %} errors{% endif%}">
  {{ render_warnings(field.field_warnings()) }}
  {{ render_errors(field.field_errors()) }}
  <legend>
    {{ field.label }}
  </legend>
  {{ help_text(field, tag="div") }}
  <ul class="radio_options options_list">
  {%- for option_key, option_label in field.choices.items() %}
    <li>
      <label class="field-option_label">
        <input type="radio" name="{{ field.field_name }}" value="{{ option_key }}"
        {%- if field.current_value() %}
          {%- if option_key in field.current_value() %} checked="checked"{% endif %}
        {%- endif -%}>
        <span class="option-display_text">
          {{ option_label }}
        </span>
      </label>
    </li>
  {% endfor -%}
  </ul>
</div>
{% endmacro %}


{% macro address_field(field) %}
<div class="field field-address_fieldset {{ field.class_name() }}
  {%- if field.field_warnings() %} warnings{% endif %}
  {%- if field.field_errors() %} errors{% endif %}">
  {{ render_errors(field.field_errors()) }}
  {{ render_warnings(field.field_warnings()) }}
  <fieldset>
    <div class="subfield address_street">
      <label class="field-wrapping_label">
        <span class="field-display_text">
          {{ field.label }}
        </span>
        {{ help_text(field) }}
        <input type="text" name="{{ field.fields.street.input_name() }}" value="{{ field.current_value().street }}">
      </label>
    </div>
    <div class="subfield-row">
      {{ basic_field(field.fields.city,  is_subfield=True) }}
      {{ basic_field(field.fields.state, is_subfield=True) }}
      {{ basic_field(field.fields.zip,   is_subfield=True) }}
    </div>
  </fieldset>
</div>
{% endmacro %}

{% macro dob_field(field) %}
<div class="field field-dob_fieldset {{ field.class_name() }}
  {%- if field.field_warnings() %} warnings{% endif %}
  {%- if field.field_warnings() %} warnings{% endif %}">
  {{ render_errors(field.field_errors()) }}
  {{ render_warnings(field.field_warnings()) }}
  <fieldset>
    <legend>{{ field.label }}</legend>
    {{ help_text(field, tag="div") }}
    <div class="subfield-row">
      {{ basic_field(field.fields.month, is_subfield=True) }}
      {{ basic_field(field.fields.day,   is_subfield=True) }}
      {{ basic_field(field.fields.year,  is_subfield=True) }}
    </div>
  </fieldset>
</div>
{% endmacro %}

{% macro basic_field(field, is_subfield=False, optional=False) %}
<div class="{% if is_subfield %}sub{% endif %}field {{ field.class_name() }}
  {%- if field.field_warnings() %} warnings{% endif %}
  {%- if field.field_errors() %} errors{% endif %}">
  {{ render_errors(field.field_errors()) }}
  {{ render_warnings(field.field_warnings()) }}
  <label class="field-wrapping_label">
    <span class="field-display_text">
      {{ field.label }}
      {% if optional -%}
      <span class="field-optional">(optional)</span>
      {%- endif %}
    </span>
    {{ help_text(field) }}
    <input type="text" name="{{ field.input_name() }}" value="{{ field.current_value() }}">
  </label>
</div>
{% endmacro %}
